package com.xywl.escort.view.order.subscribe;

import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.GetOrderInfoResp;
import com.xywl.escort.bean.SubscribeOrderResp;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

import timber.log.Timber;

/**
 * Created by 32422 on 2018/5/2.
 */

public class SubscribeOrderPresenter implements SubscribeOrderContract.Presenter {
    private SubscribeOrderContract.View mView;
    private ApiServer mApiServer;

    @Inject
    SubscribeOrderPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void takeView(SubscribeOrderContract.View view) {
        this.mView = view;
    }

    @Override
    public void loadSubscribeOrderList(int pageNum) {
        BaseReq req = new BaseReq();
        req.pageNum = pageNum;
        mApiServer.subscribeOrderList(req)
                .compose(RxUtil.<SubscribeOrderResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<SubscribeOrderResp>bindToLife())
                .subscribe(new BaseSubscriber<SubscribeOrderResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.loadSubscribeOrderListFailure();
                    }

                    @Override
                    public void onNext(SubscribeOrderResp resp) {
                        if (null != resp && null != resp.data && resp.isOk()) {
                            mView.loadSubscribeOrderListSuccess(resp.data);
                        } else {
                            mView.loadSubscribeOrderListFailure();
                        }
                    }
                });
    }

    @Override
    public void delOrder(String id, final int position) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.subscribeDelete(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.delOrderFailure("删除订单失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.delOrderSuccess(resp, position);
                            } else {
                                mView.delOrderFailure(resp.msg);
                            }
                        } else {
                            mView.delOrderFailure("删除订单失败");
                        }
                    }
                });
    }

    @Override
    public void cancelOrder(String id, final int position) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.subscribeCancel(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.cancelOrderFailure("取消订单失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.cancelOrderSuccess(resp, position);
                            } else {
                                mView.cancelOrderFailure(resp.msg);
                            }
                        } else {
                            mView.cancelOrderFailure("取消订单失败");
                        }
                    }
                });
    }

    @Override
    public void getOrderInfo(String id) {
        BaseReq req = new BaseReq();
        req.id = id;
        mApiServer.getOrderInfo(req)
                .compose(RxUtil.<GetOrderInfoResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<GetOrderInfoResp>bindToLife())
                .subscribe(new BaseSubscriber<GetOrderInfoResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.getOrderInfoFailure("获取订单信息失败");
                    }

                    @Override
                    public void onNext(GetOrderInfoResp resp) {
                        if (null != resp) {
                            if (resp.isOk() && null != resp.data) {
                                mView.getOrderInfoSuccess(resp.data);
                            } else {
                                mView.getOrderInfoFailure(resp.msg);
                            }
                        } else {
                            mView.getOrderInfoFailure("获取订单信息失败");
                        }
                    }
                });
    }

    @Override
    public void unSubscribe() {
        Timber.tag("SubscribeOrderPresenter").e("unSubscribe");
        mView = null;
    }
}
